import { defineStore } from "pinia";
import apiRequest from "@/API/entry";
import TypeSum from "@/TypeSum/entry";
import { reject } from "lodash";
import { id } from "element-plus/lib/locale";

type reqType = TypeSum['AxiosData']['AxiosData']

interface ResultType {
    treadMarkData: any[]
    cartInfoList?: any[]
    checkList?: { isChecked: number }[]
    sumSliceAndCheck?: { isCheckSum: boolean, sumPirce: number, sumNumber: number }
}
export const useStore = defineStore('ShoppingStore', {
    state: (): ResultType => ({//必须给getter名字站位才能正常访问到
        treadMarkData: []
    }),
    getters: {
        cartInfoList(): any[] {
            return this.treadMarkData.length > 0 ? this.treadMarkData[0].cartInfoList : []
        },
        checkList(state): { isChecked: number }[] {
            if (state.cartInfoList) {
                return state.cartInfoList.reduce<{ isChecked: number }[]>((_R, value) => {
                    _R.push({ isChecked: value.isChecked })
                    return _R
                }, [])
            } else {
                return []
            }
        },
        sumSliceAndCheck(state): { isCheckSum: boolean, sumPirce: number, sumNumber: number } {
            if (state.checkList && state.cartInfoList) {
                return {
                    sumNumber: state.cartInfoList.reduce<number>((_R, { isChecked, skuNum }) => {
                        if (isChecked == 1) {
                            return _R + skuNum
                        }
                        return _R
                    }, 0),
                    isCheckSum: state.checkList.every((value) => {
                        if (value.isChecked === 1) {
                            return true
                        } else {
                            return false
                        }
                    }),
                    sumPirce: state.cartInfoList.reduce<number>((_R, { skuNum, skuPrice, isChecked }) => {
                        if (isChecked == 1) {
                            return _R + skuNum * skuPrice
                        }
                        return _R
                    }, 0)

                }
            } else {
                return {
                    isCheckSum: false,
                    sumPirce: 0,
                    sumNumber: 0
                }
            }
        }
    },
    actions: {
        async getTreadMark() {//箭头函数是没有this的this指向的并不是对象 而是环境 所以会报错
            const result = await apiRequest.ApiRequest.reqTradMark() as reqType
            if (result.code == 200) {
                this.treadMarkData = result.data
                return true
            } else {
                reject(new Error('请检查网络链接'))
            }
        },
        async setIsCheck(index: number, status: Event, isSum: boolean) {
            const result = (status.target as HTMLInputElement).checked
            if (isSum) {
                (this.treadMarkData[0].cartInfoList as { isChecked: number, skuId: number }[]).forEach(async (value) => {
                    const redio = await apiRequest.ApiRequest.reqIsCheck(value.skuId, result ? 1 : 0) as TypeSum['AxiosData']['AxiosData']
                    if (redio.code == 200) {
                        value.isChecked = result ? 1 : 0
                    }
                })
            } else {
                const redio = await apiRequest.ApiRequest.reqIsCheck(this.treadMarkData[0].cartInfoList[index].skuId as number, result ? 1 : 0) as TypeSum['AxiosData']['AxiosData']
                if (redio.code == 200) {
                    this.treadMarkData[0].cartInfoList[index].isChecked = result ? 1 : 0
                }
            }
        },
        async setDelSomeGoods(index: number, sum: boolean) {
            if (sum) {

                let allPromis = []
                const listArray = this.treadMarkData[0].cartInfoList as any[]
                for (let i = 0; i < listArray.length; i++) {
                    if (this.treadMarkData[0].cartInfoList[i].isChecked == 1) {
                        allPromis.push(await apiRequest.ApiRequest.reqDelSomeGoods(listArray[i].skuId) as TypeSum['AxiosData']['AxiosData'])
                    }
                    const result = (await Promise.all(allPromis)).every((value) => {
                        if (value.code == 200) {
                            return true
                        }
                        return false
                    })
                    if (result) {
                        this.getTreadMark()
                    }
                }
            } else {
                const result = await apiRequest.ApiRequest.reqDelSomeGoods(this.treadMarkData[0].cartInfoList[index].skuId) as TypeSum['AxiosData']['AxiosData']
                if (result.code == 200) {
                    (this.treadMarkData[0].cartInfoList as any[]).splice(index, 1)
                }
            }
        }
    }
})